package ie.nolantech.gpsg;

import ie.nolantech.gpsg.GpsLoggerService.LocalBinder;
import ie.nolantech.util.StopWatch;
import ie.nolantech.util.UserPrefs;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

public class GpsGameActivity extends Activity {

	private static final String TAG = "GpsGameActivity";

	TextView mSyslog;
	TextView mLat;
	TextView mLon;

	StopWatch mClock;
	boolean started = false;

	ToggleButton mTglOnOff;

	public static final int REQ_PROX_END_OF_ROAD = 1;

	static final String UI_STORE = "ui_store";

	boolean mBound = false;
	GpsLoggerService mService;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gpsgame);

		SharedPreferences settings = getSharedPreferences(UI_STORE, 0);
		started = settings.getBoolean("started", false);

		mClock = (StopWatch) findViewById(R.id.stopWatch1);

		mTglOnOff = (ToggleButton) findViewById(R.id.tglStartStop);
		mTglOnOff.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (mTglOnOff.isChecked()) {
					if (mBound) {
						long elapsed = mService.kickOff();
						if (!started) {
							started = true;
							mClock.setBase(SystemClock.elapsedRealtime());
							mClock.startClock(elapsed);
						} else {
							mClock.restartClock();
						}
					}
				} else {
					if (mBound) {
						mService.finalWhistle();
						mClock.pauseClock();
					}
				}
			}
		});

	}

	private ServiceConnection myServiceConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.d(TAG, "onServiceConnected");
			LocalBinder localBinder = (LocalBinder) service;
			mService = localBinder.getService();
			mTglOnOff.setChecked(mService.isServiceActive());
			mBound = true;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.d(TAG, "onServiceDisconnected");
			mBound = false;
		}

	};

	@Override
	protected void onStart() {
		super.onStart();
		Intent gpsLoggerService = new Intent(GpsGameActivity.this,
				GpsLoggerService.class);
		startService(gpsLoggerService);

		bindService(gpsLoggerService, myServiceConnection, BIND_AUTO_CREATE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnu_show_result:
			Intent x = new Intent(this, ResultViewer.class);
			startActivity(x);
			break;

		case R.id.mnu_view_data:
			Intent i = new Intent(this, DbViewer.class);
			startActivity(i);
			break;
		case R.id.mnu_view_map:
			Intent j = new Intent(this, Map.class);
			startActivity(j);
			break;

		case R.id.mnu_prefs:
			Intent k = new Intent(this, UserPrefs.class);
			startActivity(k);
			break;

		case R.id.mnu_export_data:

			// String output = mHelper.exportData(this);
			Intent emailIntent = new Intent(
					android.content.Intent.ACTION_SEND_MULTIPLE);
			emailIntent.setType("application/x-gzip");
			emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
					new String[] { "anthonynolan@gmail.com" });
			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Data");
			// emailIntent.putExtra(Intent.EXTRA_TEXT, output);

			ArrayList<Uri> uris = new ArrayList<Uri>();
			File fileIn = new File("/mnt/sdcard/loc_log.csv.gz");
			Uri u = Uri.fromFile(fileIn);
			uris.add(u);

			emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

			startActivity(emailIntent);

			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onDestroy() {
		Log.d(TAG, "onDestroy");
		doUnbindService();
		super.onDestroy();
	}

	void doUnbindService() {
		if (mBound) {
			// Detach our existing connection.
			unbindService(myServiceConnection);
			mBound = false;
		}
	}

	@Override
	protected void onPause() {
		Log.d(TAG, "onPause");

		SharedPreferences settings = getSharedPreferences(UI_STORE, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean("started", started);
		editor.commit();

		super.onPause();
	}
}